#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

/* The dup() system call creates a copy of the file descriptor oldfd, using
 * the lowest-numbered unused file descriptor for the new descriptor.
 */

#define FILE_NAME "/tmp/out"

int main()
{
	int fd = open(FILE_NAME, O_CREAT|O_WRONLY|O_TRUNC, 0600);
	if (fd < 0) {
		perror("open");
		exit(1);
	}
	
	// wrong!! it is not Atomic, we should use dup2()!
	//close(1);
	dup2(fd, 1);

	if (fd != 1)
		close(fd);

	puts("hello!");
	exit(0);
}
